/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.Node;
import com.thinkgem.jeesite.modules.guobo.entity.Region;
import com.thinkgem.jeesite.modules.guobo.entity.TreeData;
import com.thinkgem.jeesite.modules.guobo.service.ExhHallService;
import com.thinkgem.jeesite.modules.guobo.service.RegionService;

import net.sf.json.JSONArray;

/**
 * 展馆Controller
 * @author wudavid
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/exhHall")
public class ExhHallController extends BaseController {

	@Autowired
	private ExhHallService exhHallService;
	
	@Autowired
	private RegionService regionService;
	
	@ModelAttribute
	public ExhHall get(@RequestParam(required=false) String id) {
		ExhHall entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = exhHallService.get(id);
		}
		if (entity == null){
			entity = new ExhHall();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:exhHall:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExhHall exhHall, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExhHall> page = exhHallService.findPage(new Page<ExhHall>(request, response), exhHall); 
		model.addAttribute("page", page);
		return "modules/guobo/exhHallList";
	}

	@RequiresPermissions("guobo:exhHall:view")
	@RequestMapping(value = "form")
	public String form(ExhHall exhHall, Model model) {
		model.addAttribute("exhHall", exhHall);
		return "modules/guobo/exhHallForm";
	}

	@RequiresPermissions("guobo:exhHall:edit")
	@RequestMapping(value = "save")
	public String save(ExhHall exhHall, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, exhHall)){
			return form(exhHall, model);
		}
		exhHallService.save(exhHall);
		addMessage(redirectAttributes, "保存展馆成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/exhHall/?repage";
	}
	
	@RequiresPermissions("guobo:exhHall:edit")
	@RequestMapping(value = "delete")
	public String delete(ExhHall exhHall, RedirectAttributes redirectAttributes) {
		exhHallService.delete(exhHall);
		addMessage(redirectAttributes, "删除展馆成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/exhHall/?repage";
	}

	/**
	 * 获取展馆JSON数据
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:exhHall:view")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ExhHall> list = exhHallService.findList(new ExhHall());
		for (int i=0; i<list.size(); i++){
			ExhHall e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", "0");
			map.put("pIds", "0,");
			map.put("name", e.getName());
			//map.put("isParent", true);
			mapList.add(map);
		}
		return mapList;
	}
	
	/**
	 * 分组的展馆JSON数据
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:exhHall:view")
	@ResponseBody
	@RequestMapping(value = "treeGroupData")
	public Map<String,Object> treeGroupData(HttpServletRequest request,HttpServletResponse response) {
		String exhallIds = request.getParameter("exhallIds");
		List<String> ids = new ArrayList<String>();
		if(exhallIds!=null && !"".equals(exhallIds)){
			ids = Arrays.asList(exhallIds.split(",")); 
		}
		Map<String,Object> data = new HashMap<String,Object>();
		List<TreeData> trees = new ArrayList<TreeData>();
		List<Region> regions = regionService.findList(new Region());
		for (Region region : regions) {
			TreeData treeData = new TreeData();
			treeData.setId(region.getId());
			treeData.setText(region.getName());
			List<ExhHall> halls = exhHallService.findExhHallByRegionId(region.getId());
			List<Node> nodes = new ArrayList<Node>();
			for (ExhHall hall : halls) {
				Node node = new Node();
				node.setId(hall.getId());
				node.setText(hall.getName());
				if(ids!=null && ids.size()>0 && ids.contains(hall.getId())){
					node.setChecked(true);
				}else{
					node.setChecked(false);
				}
				nodes.add(node);
			}
			treeData.setChildren(nodes);
			trees.add(treeData);
		}
		data.put("datas", JSONArray.fromObject(trees));
		return data;
	}
}